import 'package:flutter/material.dart';

class ACEWaterButton extends StatefulWidget {
  final double innerWidth;
  final double innerHigh;
  final double? outerWidth;
  final double? outerHigh;
  final Color color;
  final Duration? duration;

  const ACEWaterButton(
      {super.key,
      this.color = Colors.red,
      this.innerWidth = 100.0,
      this.innerHigh = 100.0,
      this.duration,
      this.outerWidth,
      this.outerHigh});

  @override
  State<ACEWaterButton> createState() => _ACEWaterButtonState();
}

class _ACEWaterButtonState extends State<ACEWaterButton> with SingleTickerProviderStateMixin {
  late AnimationController _controller;

  @override
  void initState() {
    _controller = AnimationController(vsync: this, duration: widget.duration ?? const Duration(milliseconds: 2000))
      ..repeat();
    super.initState();
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return AnimatedBuilder(
        animation: _controller,
        builder: (context, child) {
          return CustomPaint(
              size: Size(widget.outerWidth ?? widget.innerWidth * 2, widget.outerHigh ?? widget.innerHigh * 2),
              painter: ACEWaterPainter(_controller.value, widget.color, widget.innerWidth, widget.innerHigh,
                  widget.outerWidth ?? widget.innerWidth * 2, widget.outerHigh ?? widget.innerHigh * 2));
        });
  }
}

class ACEWaterPainter extends CustomPainter {
  final double progress;
  final Color color;
  final double innerWidth;
  final double innerHigh;
  final double outerWidth;
  final double outerHigh;

  final Paint _paint = Paint()..style = PaintingStyle.fill;

  ACEWaterPainter(this.progress, this.color, this.innerWidth, this.innerHigh, this.outerWidth, this.outerHigh);

  @override
  void paint(Canvas canvas, Size size) {
    _paint.color = color.withOpacity(1.0 - progress);

    double _changeW = innerWidth + (outerWidth - innerWidth) * progress;
    double _changeH = innerHigh + (outerHigh - innerHigh) * progress;

    canvas.translate(size.width / 2, size.height / 2);
    canvas.drawRRect(
        RRect.fromRectAndRadius(Rect.fromCenter(center: const Offset(0, 0), width: _changeW, height: _changeH),
            Radius.circular(outerWidth / 2)),
        _paint);
  }

  @override
  bool shouldRepaint(CustomPainter oldDelegate) => true;
}
